#include "Main.h"

VOID ChatCmdEvent(CHAR* lpszMsg)
{
	EnterCriticalSection(&Vars.cScriptSection);

	for(INT i = 0; i < Scripts.GetSize(); i++)
	{
		ScriptData* js = Scripts.ElementAt(i);

		if(!js || !JS_IsRunning(js->GetContext()))
			continue;

		JSFunction* pFunc = js->GetFunction(EVENT_CHATCMD);

		if(pFunc)
		{
			jsval argv[1];
			jsval rval;
			argv[0] = STRING_TO_JSVAL(JS_NewStringCopyZ(js->GetContext(), lpszMsg));

			JS_CallFunction(js->GetContext(), js->GetObject(), pFunc, 1, argv, &rval);
		}
	}

	LeaveCriticalSection(&Vars.cScriptSection);
}

VOID UnitLifeHitEvent(INT nAddLife, DWORD dwId1, DWORD dwId2)
{
	EnterCriticalSection(&Vars.cScriptSection);

	for(INT i = 0; i < Scripts.GetSize(); i++)
	{
		ScriptData* js = Scripts.ElementAt(i);

		if(!js || !JS_IsRunning(js->GetContext()))
			continue;

		JSFunction* pFunc = js->GetFunction(EVENT_UNITLIFEHIT);

		if(pFunc)
		{
			jsval argv[3];
			jsval rval;
			argv[0] = INT_TO_JSVAL(nAddLife);
			argv[1] = INT_TO_JSVAL(dwId1);
			argv[2] = INT_TO_JSVAL(dwId2);

			JS_CallFunction(js->GetContext(), js->GetObject(), pFunc, 3, argv, &rval);
		}
	}

	LeaveCriticalSection(&Vars.cScriptSection);
}

VOID PlayerResourceUpdateEvent(DWORD dwPlayerSlot, INT nStatId, INT nAddResource)
{
	EnterCriticalSection(&Vars.cScriptSection);

	for(INT i = 0; i < Scripts.GetSize(); i++)
	{
		ScriptData* js = Scripts.ElementAt(i);

		if(!js || !JS_IsRunning(js->GetContext()))
			continue;

		JSFunction* pFunc = js->GetFunction(EVENT_RESUPDATE);

		if(pFunc)
		{
			jsval argv[3];
			jsval rval;
			argv[0] = INT_TO_JSVAL(dwPlayerSlot);
			argv[1] = INT_TO_JSVAL(nStatId);
			argv[2] = INT_TO_JSVAL(nAddResource);

			JS_CallFunction(js->GetContext(), js->GetObject(), pFunc, 3, argv, &rval);
		}
	}

	LeaveCriticalSection(&Vars.cScriptSection);
}

VOID CreateUnitEvent(DWORD dwId1, DWORD dwId2)
{
	EnterCriticalSection(&Vars.cScriptSection);

	for(INT i = 0; i < Scripts.GetSize(); i++)
	{
		ScriptData* js = Scripts.ElementAt(i);

		if(!js || !JS_IsRunning(js->GetContext()))
			continue;

		JSFunction* pFunc = js->GetFunction(EVENT_CREATEUNIT);

		if(pFunc)
		{
			jsval argv[2];
			jsval rval;
			argv[0] = INT_TO_JSVAL(dwId1);
			argv[1] = INT_TO_JSVAL(dwId2);

			JS_CallFunction(js->GetContext(), js->GetObject(), pFunc, 2, argv, &rval);
		}
	}

	LeaveCriticalSection(&Vars.cScriptSection);
}

VOID UnitNewClassId(DWORD dwId1, DWORD dwId2, DWORD dwClassId)
{
	EnterCriticalSection(&Vars.cScriptSection);

	for(INT i = 0; i < Scripts.GetSize(); i++)
	{
		ScriptData* js = Scripts.ElementAt(i);

		if(!js || !JS_IsRunning(js->GetContext()))
			continue;

		JSFunction* pFunc = js->GetFunction(EVENT_UNITNEWCLASSID);

		if(pFunc)
		{
			jsval argv[3];
			jsval rval;
			argv[0] = INT_TO_JSVAL(dwId1);
			argv[1] = INT_TO_JSVAL(dwId2);
			JS_NewNumberValue(js->GetContext(), dwClassId, &argv[2]);

			JS_CallFunction(js->GetContext(), js->GetObject(), pFunc, 3, argv, &rval);
		}
	}

	LeaveCriticalSection(&Vars.cScriptSection);
}